package gui;

import java.awt.Desktop;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import entities.CSFileWrapper;
import entities.Device;
import entities.DeviceType;
import fileServiceAndroid.FileServiceClient;
import fileServiceWindows.FileServiceImpService;

public class DeviceOverview {
	private WindowsApplication windowsApp;
	private JPanel pnlDeviceOverview;
	private Device target;
	JList<String> lstDeviceHostFiles;
	JList<String> lstDeviceTargetFiles;
	
	public DeviceOverview(){
		initialize();
	}
	public DeviceOverview(WindowsApplication windowsApp){
		this.windowsApp = windowsApp;
		initialize();
	}
	public DeviceOverview(WindowsApplication windowsApp, Device target){
		this.windowsApp = windowsApp;
		this.target = target;
		initialize();
	}
	private void initialize(){
		pnlDeviceOverview = new JPanel();
		pnlDeviceOverview.setBounds(0, 0, 800, 600);
		pnlDeviceOverview.setLayout(null);
		
		DefaultListModel<String> listModelHost = new DefaultListModel<String>();
		for(String s : windowsApp.getFileIO().getFileNames()){
			listModelHost.addElement(s);
		}
		
		lstDeviceHostFiles = new JList<String>();
		lstDeviceHostFiles.setBounds(12, 118, 300, 400);
		lstDeviceHostFiles.setModel(listModelHost);
		pnlDeviceOverview.add(lstDeviceHostFiles);
		
		DefaultListModel<String> listModelTarget = new DefaultListModel<String>();
		if(target.getDeviceType().equals(DeviceType.WINDOWS)){
			URL url = null;
			try {
			
				url = new URL("http://" + target.getIpAddress() + ":2080/fs?wsdl");
			} catch (MalformedURLException e1) {e1.printStackTrace();
			}
			FileServiceImpService fs = new FileServiceImpService(url);
			for(String s : fs.getFileServiceImpPort().getFileNames()){
				listModelTarget.addElement(s);
			}
		}else if(target.getDeviceType().equals(DeviceType.ANDROID)){
			FileServiceClient fsc = new FileServiceClient();
			try {
				for(String s : fsc.getFileNames(target.getIpAddress())){
					listModelTarget.addElement(s);
				}
			} catch (IOException e1) {e1.printStackTrace();
			}
		}
		
		lstDeviceTargetFiles = new JList<String>();
		lstDeviceTargetFiles.setBounds(488, 118, 300, 400);
		lstDeviceTargetFiles.setModel(listModelTarget);
		pnlDeviceOverview.add(lstDeviceTargetFiles);
		
		JButton btnMoveFileToTarget = new JButton("Move File >");//host to target
		btnMoveFileToTarget.setBounds(340, 118, 120, 50);
		btnMoveFileToTarget.setFont(new Font("Arial Rounded MT Bold", Font.PLAIN, 13));
		btnMoveFileToTarget.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				CSFileWrapper hostFile = windowsApp.getFileIO().getFile(lstDeviceHostFiles.getSelectedValue());
				
				if(target.getDeviceType().equals(DeviceType.WINDOWS)){
					URL url = null;
					try {
						url = new URL("http://" + target.getIpAddress() + ":2080/fs?wsdl");
					} catch (MalformedURLException e1) {
						e1.printStackTrace();
					}
					FileServiceImpService fs = new FileServiceImpService(url);
					fs.getFileServiceImpPort().sendFile(hostFile);
				}else if(target.getDeviceType().equals(DeviceType.ANDROID)){
					FileServiceClient fsc = new FileServiceClient();
					try {
						fsc.sendFile(hostFile, target.getIpAddress());
					} catch (UnknownHostException e1) {e1.printStackTrace();
					} catch (IOException e1) {e1.printStackTrace();
					}
				}
				
				windowsApp.getFrame().setContentPane(new DeviceOverview(windowsApp, target).getPanel());
			}
		});
		pnlDeviceOverview.add(btnMoveFileToTarget);
		
		JButton btnMoveFileToHost = new JButton("Move File <");
		btnMoveFileToHost.setBounds(340, 173, 120, 50);
		btnMoveFileToHost.setFont(new Font("Arial Rounded MT Bold", Font.PLAIN, 13));
		btnMoveFileToHost.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				System.out.println("Move to Host");
				//from target to host
				CSFileWrapper targetFile = null;
				if(target.getDeviceType().equals(DeviceType.WINDOWS)){
					URL url = null;
					try {
					
						url = new URL("http://" + target.getIpAddress() + ":2080/fs?wsdl");
					} catch (MalformedURLException e1) {e1.printStackTrace();
					}
					FileServiceImpService fs = new FileServiceImpService(url);
					targetFile = fs.getFileServiceImpPort().getFile(lstDeviceTargetFiles.getSelectedValue());
				}else if(target.getDeviceType().equals(DeviceType.ANDROID)){
					FileServiceClient fsc = new FileServiceClient();
					
					try {
						targetFile = fsc.getFile(target.getIpAddress(), lstDeviceTargetFiles.getSelectedValue());
					} catch (UnknownHostException e1) {e1.printStackTrace();
					} catch (IOException e1) {e1.printStackTrace();
					}
					
				}
				
				windowsApp.getFileIO().writeFile(targetFile);
				windowsApp.getFrame().setContentPane(new DeviceOverview(windowsApp, target).getPanel());
			}
		});
		pnlDeviceOverview.add(btnMoveFileToHost);
		
		JButton btnPreviewFileHost = new JButton("Preview File");
		btnPreviewFileHost.setBounds(12, 537, 130, 50);
		btnPreviewFileHost.setFont(new Font("Arial Rounded MT Bold", Font.PLAIN, 13));
		btnPreviewFileHost.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				CSFileWrapper hostFile = windowsApp.getFileIO().getFile(lstDeviceHostFiles.getSelectedValue());
				
				
				File file = new File("/Capstone/Share/" + hostFile.getFileName());
				try {
					Desktop.getDesktop().open(file);
				} catch (IOException e1) {e1.printStackTrace();
				}
			}
		});
		pnlDeviceOverview.add(btnPreviewFileHost);
		
		JButton btnPreviewFileTarget = new JButton("Preview File");
		btnPreviewFileTarget.setBounds(658, 537, 130, 50);
		btnPreviewFileTarget.setFont(new Font("Arial Rounded MT Bold", Font.PLAIN, 13));
		btnPreviewFileTarget.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				CSFileWrapper targetFile = null;
				if(target.getDeviceType().equals(DeviceType.WINDOWS)){
					URL url = null;
					try {					
						url = new URL("http://" + target.getIpAddress() + ":2080/fs?wsdl");
					} catch (MalformedURLException e1) {e1.printStackTrace();
					}
					FileServiceImpService fs = new FileServiceImpService(url);
					targetFile = fs.getFileServiceImpPort().getFile(lstDeviceTargetFiles.getSelectedValue());
				}else if(target.getDeviceType().equals(DeviceType.ANDROID)){
					FileServiceClient fsc = new FileServiceClient();
					
					try {
						targetFile = fsc.getFile(target.getIpAddress(), lstDeviceTargetFiles.getSelectedValue());
					} catch (UnknownHostException e1) {e1.printStackTrace();
					} catch (IOException e1) {e1.printStackTrace();
					}					
				}
				windowsApp.getFileIO().writePreviewFile(targetFile);
				
				File file = new File("/Capstone/Temp/" + targetFile.getFileName());
				try {
					Desktop.getDesktop().open(file);
				} catch (IOException e1) {e1.printStackTrace();
				}
			}
		});
		pnlDeviceOverview.add(btnPreviewFileTarget);
		
		JButton btnBack = new JButton("Back");
		btnBack.setBounds(340, 550, 120, 25);
		btnBack.setFont(new Font("Arial Rounded MT Bold", Font.PLAIN, 13));
		btnBack.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){				
				windowsApp.getFileIO().cleanTempFolder();
				windowsApp.getFrame().setContentPane(new DeviceList(windowsApp).getPanel());
			}
		});
		pnlDeviceOverview.add(btnBack);
		
		JLabel lblDeviceNameHost = new JLabel("Host: " + windowsApp.getDevice().getDeviceName());
		lblDeviceNameHost.setBounds(12, 13, 200, 22);
		lblDeviceNameHost.setFont(new Font("Arial Rounded MT Bold", Font.PLAIN, 13));
		pnlDeviceOverview.add(lblDeviceNameHost);
		
		JLabel lblIpAddressHost = new JLabel(windowsApp.getDevice().getIpAddress());
		lblIpAddressHost.setBounds(12, 45, 200, 22);
		lblIpAddressHost.setFont(new Font("Arial Rounded MT Bold", Font.PLAIN, 13));
		pnlDeviceOverview.add(lblIpAddressHost);
		
		System.out.println(target.toString());
		
		JLabel lblDeviceNameTarget = new JLabel("Target: " + target.getDeviceName());
		lblDeviceNameTarget.setBounds(588, 13, 200, 22);
		lblDeviceNameTarget.setHorizontalAlignment(SwingConstants.RIGHT);
		lblDeviceNameTarget.setFont(new Font("Arial Rounded MT Bold", Font.PLAIN, 13));
		pnlDeviceOverview.add(lblDeviceNameTarget);
		
		JLabel lblIpAddressTarget = new JLabel(target.getIpAddress());
		lblIpAddressTarget.setBounds(588, 45, 200, 22);
		lblIpAddressTarget.setHorizontalAlignment(SwingConstants.RIGHT);
		lblIpAddressTarget.setFont(new Font("Arial Rounded MT Bold", Font.PLAIN, 13));
		pnlDeviceOverview.add(lblIpAddressTarget);
	}
	
	public JPanel getPanel(){
		return pnlDeviceOverview;
	}
	public void setDevice(Device target){
		this.target = target;
	}
}
